<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Factory Injection</title></head>

<body>
<h3>Overview</h3>

<p>This allows an instance to be injected via a factory that is
    aware of the thing it is injecting into and can make a custom instance
    just for that injectee. This type of injection is only possible if you
    add an Adapter for it directly that subclasses <strong>FactoryInjection&lt;T&gt;</strong></p>

<div class="source">
<pre>public class Apple {
    private final Log log;
    private final Orange orange;
    private final Pear pear;
    private final Banana banana;

    public Apple(Orange orange, Pear pear, Banana banana, Log log) {
        this.orange = orange;
        this.pear = pear;
        this.banana = banana;
        this.log = log;
    }

    // methods
}</pre>
</div>
<h3>Usage</h3>

<div class="source">
<pre>public class LogInjector extends FactoryInjector&lt;Log&gt; {
    public Log getComponentInstance(PicoContainer container, final Type into) throws PicoCompositionException {
        return LogFactory.getLog((Class) into);
    }
}
...
pico = new DefaultPicoContainer(new ConstructorInjection());
pico.addComponent(Apple.class);
pico.addAdapter(new LogInjector());
// etc
Apple apple = pico.getComponent(Apple.class);
</pre>
</div>
<p>
    We have implementations in this style for Log4j, Commons-Logging, Java-Logging and SLF4J. They are in the
    org.picocontainer.gems.injectors package.
</p>

</body>
</html>